package com.ut.demo.slice;

import com.clock.scratch.ScratchView;
import com.clock.scratch.util.LogUtil;
import com.ut.demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice implements Slider.ValueChangedListener{

    private ScratchView scratchView;

    private Text percentView;

    private Slider eraseSize, maxPercent;



    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        scratchView = (ScratchView) findComponentById(ResourceTable.Id_scratchView);
        percentView = (Text) findComponentById(ResourceTable.Id_percent);
        eraseSize = (Slider) findComponentById(ResourceTable.Id_erase_size_progress);
        maxPercent = (Slider) findComponentById(ResourceTable.Id_max_percent_progress);
        RadioContainer colorsGroup = (RadioContainer) findComponentById(ResourceTable.Id_colors);
        RadioContainer watermarkGroup = (RadioContainer) findComponentById(ResourceTable.Id_watermark);
        scratchView.setEraseStatusListener(new ScratchView.EraseStatusListener() {
            @Override
            public void onProgress(int percent) {
                percentView.setText(String.valueOf(percent));
            }

            @Override
            public void onCompleted(Component component) {
                LogUtil.i("execute onCompleted");
            }
        });
        colorsGroup.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {

            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int position) {
                LogUtil.i("onCheckedChanged position: %d", position);
                if (position == 0) {
                    scratchView.setMaskColor(0xffff0000);
                } else if (position == 1) {
                    scratchView.setMaskColor(0xff00ff00);
                } else if (position == 2) {
                    scratchView.setMaskColor(0xff0000ff);
                } else if (position == 3) {
                    scratchView.setMaskColor(0xffcccccc);
                }
            }

        });
        watermarkGroup.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {

            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int position) {
                LogUtil.i("onCheckedChanged position: %d", position);
                if (position == 0) {
                    scratchView.setWatermark(ResourceTable.Media_alipay);
                } else if (position == 1) {
                    scratchView.setWatermark(-1);
                }
            }

        });
        findComponentById(ResourceTable.Id_clear).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                scratchView.clear();
            }
        });
        findComponentById(ResourceTable.Id_reset).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                scratchView.reset();
            }
        });
        eraseSize.setValueChangedListener(this);
        maxPercent.setValueChangedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onProgressUpdated(Slider slider, int progress, boolean b) {
        LogUtil.i("onProgressUpdated: %d, value: %d, boolean: %b", slider.getId(), progress, b);
        switch (slider.getId()){
            case ResourceTable.Id_erase_size_progress:
                scratchView.setEraserSize(progress);
                break;
            case ResourceTable.Id_max_percent_progress:
                scratchView.setMaxPercent(progress);
                break;
        }
    }


    @Override
    public void onTouchStart(Slider slider) {
        LogUtil.i("onTouchStart: %d", slider.getId());
    }

    @Override
    public void onTouchEnd(Slider slider) {
        LogUtil.i("onTouchEnd: %d", slider.getId());
    }
}
